package com.srwl.mytx.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Checkable;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.srwl.mytx.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by dahai2070 on 2016/11/26 0026.
 */
public class GoodsClassfyCheckableItem extends LinearLayout implements Checkable {

    @BindView(R.id.tv_classfy_item)
    TextView tv_classfyItem;

    private static final int[] CHECKED_STATE_SET = {
            android.R.attr.state_checked
    };
    // 是否选中
    private boolean mChecked = false;
    Context context;

    public GoodsClassfyCheckableItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.goods_classfy_item_layout, this, true);
        ButterKnife.bind(this, v);
    }

    public void setData(String data) {
        tv_classfyItem.setText(data);
    }

    @Override
    protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
        return super.checkLayoutParams(p);
    }

    @Override
    public void setChecked(boolean checked) {

        if (checked) {
            tv_classfyItem.setBackgroundResource(R.color.placeholder);
            tv_classfyItem.setTextColor(context.getResources().getColor(R.color.white));
        } else {

            tv_classfyItem.setBackgroundResource(R.color.white);
            tv_classfyItem.setTextColor(context.getResources().getColor(R.color.gray_normal));
        }
        mChecked = checked;
    }

    @Override
    public boolean isChecked() {
        return mChecked;
    }

    @Override
    public void toggle() {
        setChecked(!mChecked);
    }
}
